package com.cm367.teamc;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import com.cm367.teamc.bean.User;
import com.cm367.teamc.message.EMessage;
import com.cm367.teamc.service.UserService;
import com.cm367.teamc.session.Language;
import com.cm367.teamc.session.UserSession;

@ManagedBean
public class CreateAccount {

	private String userID;
	private String userName;
	private String role;
	private String department;
	private String password;

	private String result;

	private List<User> users;

	@ManagedProperty(value = "#{eMessage}")
	private EMessage eMessage;

	@ManagedProperty(value = "#{userService}")
	private UserService userService;

	@ManagedProperty(value = "#{userSession}")
	private UserSession userSession;

	@ManagedProperty(value = "#{language}")
	private Language language;

	public EMessage geteMessage() {
		return eMessage;
	}

	public void seteMessage(EMessage eMessage) {
		this.eMessage = eMessage;
	}

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	public String getUserID() {
		return userID;
	}

	public void setUserID(String userID) {
		this.userID = userID.toLowerCase();
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getpassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserSession getUserSession() {
		return userSession;
	}

	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

	public String getResult() {
		return result;
	}

	public String create() {
		String result = userService.create(userID, password, role, userName,
				department);
		if (result.equals("exist")) {
			this.result = eMessage.existUserID(userID);
		} else if (result.equals("createSuccess")) {
			this.result = eMessage.createSuccess(userID);
		} else if (result.equals("Manager Not Found")) {
			this.result = eMessage.addManagerFirst();
		} else if (result.equals("Manager Exist")) {
			this.result = eMessage.existManager();
		} else {
			this.result = eMessage.tryLater();
		}
		return null;
	}

	public List<User> getUsers() {
		users = userService.findAllUser();
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String deleteUser() {
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String, String> params = fc.getExternalContext()
				.getRequestParameterMap();
		String userID = params.get("UID");
		if (userService.delete(userID)) {
			return null;
		}
		return null;
	}

}
